// pages/register/register.js
var app = getApp();
var main = require("../../main.js");
const config = app.globalData;
Page({

    /**
     * 页面的初始数据
     */
    data: {
      userInfo:null,
      isGrant: false,
      truename: '',
      mobile: '',
      company_name: '',
      career_name: '',
      email: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.checkUserAuthorization();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    // 编辑处理
    registerRquest: function() {
      wx.showLoading({
        title: '加载中...',
      });
      wx.request({
        url: app.domain_path + app.api_path + '/update_member',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        data: {
          token: main.get_storage('token'),
          mobile: this.data.mobile,
          truename: this.data.truename,
          company_name: this.data.company_name,
          career_name: this.data.career_name,
          email: this.data.email
        },
        success: function(res) {
          wx.hideLoading();
          if (!res.data) {
            wx.showToast({
              title: '数据错误',
              icon: 'loading',
              duration: 3000
            })
          }
          if (res.data.RESULT == 'OK') {
            wx.showToast({
              title: "修改成功",
              icon: 'none',
              duration: 3000
            })
          } else {
            wx.showToast({
              title: res.data.MESSAGE,
              icon: 'none',
              duration: 3000
            })
          }
        },
        fail: err => {
          wx.hideLoading();
          wx.showToast({
            title: err,
            icon: 'none'
          });
        }
      })
    },

    checkUserAuthorization: function () {
      const that = this;
      // 获取用户的授权信息
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            console.log(main.get_storage('token') != '');
            if(main.get_storage('token') != []){
              //获取个人账户信息
              that.get_member_info();
            }else{
              this.jumpConfirm();
            }
          } else {
            this.jumpConfirm();
          }
        },
        fail: (err) => {
          wx.showToast({
            title: err,
            icon: 'none'
          });
        }
      });
    },
    jumpConfirm:function(){
      wx.showModal({
        title: '温馨提示',
        content: '此功能需要授权登录，请确认~',
        success: (res) => {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/index/index'
            });
          } else if (res.cancel) {
            wx.switchTab({
              url: '/pages/ser-list/ser-list',
            });
          }
        },
      });
    },

   /**
     * 获取个人账户信息
     */
      get_member_info: function() {
        var that = this;
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.domain_path + app.api_path + '/member_info',
          method: 'post',
          data: {
            token: main.get_storage('token'),
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            if (!res.data) {
              wx.showToast({
                title: '加载错误',
                icon: 'loading',
                duration: 3000
              })
            }
            if (res.data.RESULT == 'OK') {
              wx.hideLoading();
              that.setData({
                isGrant: true,
                userinfo: res.data.data.member_info,
                truename: res.data.data.member_info.truename,
                mobile: res.data.data.member_info.mobile,
                company_name: res.data.data.member_info.company_name,
                career_name: res.data.data.member_info.career_name,
                email: res.data.data.member_info.email,
              })
            } else {
              wx.showToast({
                title: res.data.MESSAGE,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
      },


      cancelRquest: function() {
        wx.setStorageSync('refreshTabPage', true);
        wx.switchTab({
          url: '/pages/mine/mine'
        });
      },
})